/**
 * Created by HUANGFR2 on 8/24/2016.
 */
angular.module('merchant',['ngAnimate'])
    .controller('dishController', function ($scope, $routeParams, $http) {

    var mid = $routeParams.id;
    var dishes ;
    $http.get('/merchants/'+mid)
        .then(function (res) {
            var data = res.data;
            if(data.msg == 'SUCCESS'){

                $scope.merchant = data.merchant;
                dishes = data.merchant.dish;
                var req = {
                    method:"GET",
                    url:"/cart/getCart/"+mid
                };
                $http(req)
                    .then(function(res){
                        if(res.data.msg == "SUCCESS"){
                            cart = res.data.carts;
                            //console.log(cart)
                            cart.forEach(function(c){
                                dishes.forEach(function(d){
                                    if(c.did == d._id){
                                        c.dname = d.dname;
                                        c.price = d.price;
                                        c.img = d.img;
                                    }
                                })
                            })
                            $scope.cart = cart;
                        }
                    })
            } else {

            }
        }, function (res) {
            
        })

    var cart = [];
    $scope.cart = cart;
    //$scope.dishes = dishes;
    //取出数据
    var newIndex;
    $scope.addToCart = function(dish){
        //console.log(dish)
        $scope.visible = false;
        var exist = cart.some(function(oldCart,inx,array){
            newIndex = inx;
            return angular.equals(oldCart.did,dish._id);

        })
        if(!exist){
            var req = {
                method: "post",
                url: "/cart/addCart/"+mid,
                data: {
                    did:dish._id,dname:dish.dname,price:dish.price,img:dish.img,num:1
                }
            }
            $http(req)
                .then(function(res){
                    var data = res.data;
                    if(data.msg == 'SUCCESS') {
                        var cartItem = {did:dish._id,dname:dish.dname,price:dish.price,img:dish.img,num:1}
                        cart.push(cartItem);
                    } else {
                        window.location.href = '/login.html'
                    }
                })



        }else{
            var num = cart[newIndex].num + 1;
            var did = cart[newIndex].did
            var req = {
                method:"put",
                url:"/cart/updateCart/"+mid,
                data:{did:did,num:num}
            }
            $http(req)
                .then(function(res){
                    if(res.data.msg == "SUCCESS"){
                        cart[newIndex].num = num;
                    }else{
                        console.log("更新数据失败");
                    }
                })
        }

    }
    $scope.updateCart = function(index,type){
        if(type == "reduce"){
            var num = cart[index].num - 1;
            var did = cart[index].did
            var req = {
                method:"put",
                url:"/cart/updateCart/"+mid,
                data:{did:did,num:num}
            }
            $http(req)
                .then(function(res){
                    if(res.data.msg == "SUCCESS"){
                        cart[index].num = num;
                    }else{
                        console.log("更新数据失败");
                        window.location.href = '/login.html'
                    }
                })
        }else{
            var num = cart[index].num + 1;
            //console.log(cart[index].num)
            var did = cart[index].did;
            var req = {
                method:"put",
                url:"/cart/updateCart/"+mid,
                data:{did:did,num:num}
            }
            $http(req)
                .then(function(res){
                    if(res.data.msg == "SUCCESS"){
                        cart[index].num++;
                    }else{
                        console.log("更新数据失败");
                        window.location.href = '/login.html'
                    }
                })
        }
    }
    $scope.$watch("cart",function(newCart,oldCart){
        $scope.total = 0;
        for(var i=0;i<cart.length;i++){
            $scope.total += (cart[i].num *cart[i].price);
            if(cart[i].num == 0){
                cart.splice(i,1);
            }

        }
    },true);

        $scope.visible = true;
        $scope.toggle = function () {
            $scope.visible = !$scope.visible;
        }
})
    //cart animation about in and out
.animation('.hide-animation', function () {
    return {
        beforeAddClass : function(element, className, done) {
            //console.log(element)
            if (className === 'ng-hide') {
                element.animate({
                    right:'-20%'
                },300, done);
                angular.element("#cartNav").animate({
                    right : '0%'
                }, 300);
            } else {
                done();
            }
        },
        removeClass : function(element, className, done) {
            if (className === 'ng-hide') {
                //element.css('opacity',0);
                element.animate({
                    right:'0%'
                }, 300, done);
                angular.element("#cartNav").animate({
                    right : '20%'
                }, 300);
            } else {
                done();
            }
        }
    };
})
//cart animation about picture
    .animation('.picture-animation', function () {
        return {
            move : function(element, className, done) {
                console.log(element)
                if (className === 'ng-hide') {
                    element.animate({
                        left:0
                    },500, done);
                    angular.element("#cartNav").animate({
                        left:100
                    }, 500);
                } else {
                    done();
                }
            }
        };
    })
